package com.zjj.lbw.designpattern.singleton;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author zhanglei.zjj
 * @description 枚举方式实现 懒汉单例模式
 * @date 2023/7/11 18:05
 */
public class EnumLazySingletonTest {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Constructor<EnumLazySingleton.EnumHolder> constructor = EnumLazySingleton.EnumHolder.class.getDeclaredConstructor(String.class, int.class);

        constructor.setAccessible(true);
        constructor.newInstance("instance", 0);

    }
}

class EnumLazySingleton {
    private EnumLazySingleton() {
    }

    public enum EnumHolder {
        INSTANCE;
        private final EnumLazySingleton instance;

        EnumHolder() {
            instance = new EnumLazySingleton();
        }

        private EnumLazySingleton getInstance() {
            return instance;
        }
    }

    public static EnumLazySingleton getInstance() {
        return EnumHolder.INSTANCE.getInstance();
    }
}
